package com.example.AIstudy.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.example.AIstudy.domain.Exercise;
import com.example.AIstudy.domain.ProSet;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProSetDao extends BaseMapper<ProSet> {


    //新增一个套题的信息，并且实现id回填
    @Insert("INSERT INTO proset(proSetName,proSetDesc,stuID,allscore,prosetLevel,aiUse) " +
            "VALUES (#{proSetName},#{proSetDesc},#{stuID},#{allscore},#{prosetLevel},#{aiUse})")
    @SelectKey(statement = "select last_insert_id()", keyProperty = "id",
            keyColumn = "id", resultType = Integer.class, before = false)
    void insertProSet(ProSet proSet);


    //分页获取套题的信息，这里可以直接封装套题所包含题的id
    @Select("SELECT * FROM proset ${ew.customSqlSegment}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "exerciseList", column = "id",
                    one = @One(select = "com.example.AIstudy.dao.SetAndExeDao.getExeIDBySetID")),
    })
    public IPage<ProSet> findProset(IPage<ProSet> page,
                                        @Param(Constants.WRAPPER) QueryWrapper<ProSet> wrapper);


    //分页获取班级的套题信息，这里可以直接封装套题所包含题的id
//    @Select("SELECT p.*,q.startTime,q.endTime FROM proset as p,prosetandclass as q ${ew.customSqlSegment} and p.id=q.prosetID")
//    public IPage<ClassProsetDTO> findProsetByClassID(IPage<ClassProsetDTO> page,
//                                                     @Param(Constants.WRAPPER) QueryWrapper<ClassProsetDTO> wrapper);
//    @Select("SELECT p.*,q.startTime,q.endTime FROM proset as p,prosetandclass as q where p.id=#{prosetID} and p.id=q.prosetID")
//    public ClassProsetDTO findProsetByProsetID(int prosetID);

    //获取套题包含的题目信息
    @Select("SELECT p.*" +
            "FROM exercise as p,setandexe as q where prosetID=#{prosetID} and p.id=q.exerciseID and p.category=#{category} and p.isSingle=true ")
    public List<Exercise> findProsetSingleExe(int prosetID,String category);

    @Select("SELECT p.* " +
            "FROM exercise as p,setandexe as q where prosetID=#{prosetID} and p.id=q.exerciseID and p.category=#{category} and p.sonProID!=\"\"")
    public List<Exercise> findProsetNoSingleExe(int prosetID,String category);

}
